<?PHP
/*   This is a BSD style permissive license.
*    This module is original work by the author.
*
* Copyright (c) 2007, iNetOffice, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of iNetOffice nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY iNetOffice ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL iNetOffice BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Author: Tom Snyder, iNetOffice, Inc.
*/

/* opensam_trysso.php -- Do an SSO log in to a webdav server.
 *
 * This page posts back to itself and the does a redirect to the file browser
 * module if successful.
 */


require_once( "opensam_sso.php" );
 
// we use php default session handling services:
session_start();

if( !isset( $_SESSION ) ) $_SESSION = array(); // to avoid warnings and hassles.

// The $g_login_state_message tells the user their current logged in state or reports errors.
// The defaul logged-in message is the current state of the logged in session var:
if( empty( $_SESSION['state'] ) || $_SESSION['state'] != "loggedin" ) {
	$g_login_state_message = "You are <font color=red><strong>Logged Out</strong></font> of the sample login and browsing UI below.";
} else {
	$g_login_state_message = "You are <font color=#00A000><strong>Logged In</strong> as '".$_SESSION['StorageUserName']."'</font> to the sample login and browsing UI below.";
}


// These are out params:
$g_StorageServerUrl = $g_StorageUserName = $g_StorageSessionId = $g_StoragePassword = $g_WebDAVUrlParameters = null;

// Attempt an SSO log in using the opensam SSO helper. The helper uses the SSO CGI parameters.
// The SSO CGI parameters might be present in a variety of requests, so we check for them every time.
$sso_ret = opensam_sso_authenticate( $g_StorageServerUrl, $g_StorageUserName, $g_StorageSessionId, $g_StoragePassword, 
  $g_StorageDomainToConfirm, $g_WebDAVUrlParameters, $g_HTTPStatus );
  
if( empty( $g_StorageServerUrl ) && !empty($_GET['StorageServerUrl']) ) $g_StorageServerUrl = $_GET['StorageServerUrl'];
if( empty( $g_StorageUserName )  && !empty($_GET['StorageUserName']) )  $g_StorageUserName  = $_GET['StorageUserName'];
if( empty( $g_StorageSessionId ) && !empty($_GET['StorageSessionId']) ) $g_StorageSessionId = $_GET['StorageSessionId'];
if( empty( $g_StoragePassword )  && !empty($_GET['StoragePassword']) )  $g_StoragePassword  = $_GET['StoragePassword'];

if( array_key_exists( 'Logout', $_GET ) ) {
  clear_login_session();
}
else if( array_key_exists( 'Login', $_GET ) ) {
  if( $sso_ret === 0 ) {
	  ; // NO SSO ATTEMPT MADE. No CGI parameters values found.
	  $report_msg = "LOGIN FAILED. Please enter login values into the fields below.";
  } 
  else if( $sso_ret === true ) {
	  // Successful login, establish the session:
	  $_SESSION['state'] = 'loggedin';
	  $_SESSION['StorageServerUrl'] = $g_StorageServerUrl;
	  $_SESSION['StorageUserName']  = $g_StorageUserName;
	  $_SESSION['StorageSessionId'] = $g_StorageSessionId;  
	  $_SESSION['StoragePassword']  = $g_StoragePassword;  
	  if( !empty( $g_WebDAVUrlParameters ) ) $_SESSION['WebDAVUrlParameters'] = $g_WebDAVUrlParameters;
	  else unset( $_SESSION['WebDAVUrlParameters'] ); // no leftovers please.
  	
	  $g_login_state_message = "Log in succeeded (HTTP status $g_HTTPStatus). Storage Domain and path to confirm for account is '$g_StorageDomainToConfirm'<br>";
	  $g_login_state_message .= "You are <font color=#00A000><strong>Logged In</strong></font> as '$g_StorageUserName'</font> to the sample login and browsing UI below.";
	  header( "Location: opensam_tryfilebrowse.php" );
	  exit;
  }
  else {
    if( $sso_ret === false ) {
      $report_msg = "LOGIN FAILED. Incorrect User Name or Password.";
    }
    else {
      $report_msg = $sso_ret;  // the ret is the detailed err msg
    }
	  // SSO login attempt failed:
	  clear_login_session();
	  $g_login_state_message = "<font style=\"background-color:yellow;\">".$report_msg."</font>";
  }
}

function clear_login_session() {
  $_SESSION['state'] = 'loggedout';
  unset( $_SESSION['StorageServerUrl'] );
  unset( $_SESSION['StorageSessionId'] );
  unset( $_SESSION['StoragePassword'] );
  unset( $_SESSION['StorageUserName'] );
  unset( $_SESSION['WebDAVUrlParameters'] );
}

?>
<html><head><title>OpenSAM SSO</title>
<STYLE title=inetWord type=text/css>TD.iNetwordTDBrdr {
	BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.5pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.5pt; VERTICAL-ALIGN: top; BORDER-LEFT: black 1pt solid; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; netwrd-type: internal
}
TD.iNetwordTD {
	PADDING-RIGHT: 5.5pt; PADDING-LEFT: 5.5pt; VERTICAL-ALIGN: top; BACKGROUND-COLOR: transparent; netwrd-type: internal
}
TABLE.iNetwordTABLE {
	BORDER-COLLAPSE: collapse; netwrd-type: internal
}
H1.MsoNormal {
	FONT-WEIGHT: bold; FONT-SIZE: 16pt; MARGIN: 12pt auto 6pt; FONT-FAMILY: Arial; mso-style-name: "Heading 1"
}
H2.MsoNormal {
	FONT-WEIGHT: bold; FONT-SIZE: 14pt; MARGIN: 12pt auto 3pt; FONT-STYLE: italic; FONT-FAMILY: Arial; mso-style-name: "Heading 2"
}
H3.MsoNormal {
	FONT-WEIGHT: bold; FONT-SIZE: 13pt; MARGIN: 12pt auto 3pt; FONT-FAMILY: Arial; mso-style-name: "Heading 3"
}
.iNetwordPageBreak {
	DISPLAY: block; BACKGROUND-IMAGE: url(/s/i/bg_pagebreak.gif); PAGE-BREAK-BEFORE: always; WIDTH: 100%; HEIGHT: 10px; netwrd-type: internal
}
TD {
	netwrd-type: internal
}
.MsoNormal, .l, .r {
	MARGIN-TOP: 0in; FONT-WEIGHT: 400; FONT-SIZE: 11pt; MARGIN-BOTTOM: 0in; TEXT-INDENT: 0pt; FONT-STYLE: normal; FONT-FAMILY: Tahoma; TEXT-ALIGN: left; mso-style-name: "Normal"
}
.l { PADDING-RIGHT:4px; TEXT-ALIGN:right; background-color: #9ACF19;
}
.halfabove {
	MARGIN-TOP: 5pt; FONT-WEIGHT: 400; FONT-SIZE: 11pt; MARGIN-BOTTOM: 0in; TEXT-INDENT: 0pt; FONT-STYLE: normal; FONT-FAMILY: Tahoma; TEXT-ALIGN: left; mso-style-name: "Normal"
}
.modest {
	MARGIN-TOP: 0in; FONT-WEIGHT: 400; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0in; TEXT-INDENT: 0pt; FONT-STYLE: normal; FONT-FAMILY: Tahoma; TEXT-ALIGN: left; mso-style-name: "Normal"
}
.error {
	PADDING: 4px; FONT-SIZE: 11pt; TEXT-INDENT: 0pt; FONT-STYLE: normal; FONT-FAMILY: Tahoma; TEXT-ALIGN: left; background-color:yellow
}
</STYLE>
<script type="text/javascript">
function useourserver() {
  var input_ServerUrl = document.getElementById( "StorageServerUrlID" );
  var input_UserName  = document.getElementById( "StorageUserNameID" );
  var input_SessionId  = document.getElementById( "StorageSessionId" );
  var input_Password  = document.getElementById( "StoragePassword" );
  input_ServerUrl.value = "http://webdav.inetapps.com";
  input_UserName.value = "johndoe";
  input_Password.value = "donttell";
  input_SessionId.value = "";
}
</script>
</head>
<body style="margin:0px; padding:0px">
<DIV style="background-image: url( http://www.opensam.org/pics/opensam%20logos/opensource_tools_bg.png )">
<IMG src="http://www.opensam.org/pics/opensam%20logos/opensource_tools_bg.png"></DIV>
<DIV style="padding:48px 36px 36px 36px">
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>
<TABLE class=iNetwordTABLE style="MARGIN-LEFT: 0px; MARGIN-RIGHT: auto; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD class=iNetwordTD style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px">
<P style="MARGIN-BOTTOM: 0px"><A href="//www.opensam.org"><IMG height=65 src="http://www.opensam.org/pics/opensam%20logos/opensam_button_140x.png" width=140 border=0 x_orig_height="65" x_orig_width="140"></A></P></TD>
<TD class=iNetwordTD style="PADDING-RIGHT: 5.5pt; PADDING-LEFT: 30px; VERTICAL-ALIGN: middle">
<H1 class=MsoNormal style="MARGIN-TOP: 0pt;">OpenSAM Single Sign On (SSO)</H1>
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>
<TABLE class=iNetwordTABLE style="MARGIN-LEFT: 0px; MARGIN-RIGHT: auto; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD class=iNetwordTD>
<P class=modest style="TEXT-ALIGN: right" align=right>specification:</P></TD>
<TD class=iNetwordTD>
<P class=modest><A href="//www.opensam.org/4.1%20Application%20Launch.html">4.1 Application Launch</A></P></TD></TR>
<TR>
<TD class=iNetwordTD>
<P class=modest style="TEXT-ALIGN: right" align=right>downloads, php:</P></TD>
<TD class=iNetwordTD>
<P class=modest><a href="opensam_sso.php.txt">opensam_sso.php</a>&nbsp;&nbsp;&nbsp;<a href="opensam_trysso.php.txt">opensam_trysso.php (this page)</a>
<TR>
<TD class=iNetwordTD>
<P class=modest style="TEXT-ALIGN: right" align=right>downloads, java:</P></TD>
<TD class=iNetwordTD>
<P class=modest><a href="OpenSAM_SSO.java.txt">OpenSAM_SSO.java</a>&nbsp;&nbsp;&nbsp;<a href="testharness_OpenSAM_SSO.jsp.txt">testharness_OpenSAM_SSO.jsp (simple use example)</a>
</P></TD></TR>
</P></TD></TR></TBODY></TABLE></DIV>
</TD></TR></TBODY></TABLE></DIV>
<form name=frm action="opensam_trysso.php" method=GET style="margin-left:36px">
<p class=MsoNormal>&nbsp;</p>
<h3 class=MsoNormal>Log in using OpenSAM SSO</h3>
<P class=MsoNormal><small>This will bring up the OpenSAM file browser.</small></P>
<P class=MsoNormal >&nbsp;</P>
<?PHP
  if( !empty($report_msg) ) {
    print "<P class=MsoNormal style=\"color:red\"><B>" . htmlentities( $report_msg ) . "</B></P>";
    print "<P class=MsoNormal>&nbsp;</P>\n";
  }
?>

<P class=MsoNormal >StorageServerUrl:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StorageServerUrl id=StorageServerUrlID style="width:3in"
<?PHP if( !empty( $g_StorageServerUrl ) ) print "value=\"$g_StorageServerUrl\""; ?>
>&nbsp;&nbsp;&nbsp;<a href="javascript:useourserver()">use our test server</a></P>
<P class=halfabove >StorageUserName:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StorageUserName id=StorageUserNameID style="width:3in"
<?PHP if( !empty( $g_StorageUserName ) ) print "value=\"$g_StorageUserName\""; ?>
></P>
<P class=halfabove >StorageSessionId:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StorageSessionId id=StorageSessionIdID style="width:3in"
<?PHP if( !empty( $g_StorageSessionId ) ) print "value=\"$g_StorageSessionId\""; ?>
></P>
<P class=MsoNormal >or, StoragePassword:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StoragePassword id=StoragePasswordID style="width:3in"
<?PHP if( !empty( $g_StoragePassword ) ) print "value=\"$g_StoragePassword\""; ?>
></P>
<p class=MsoNormal>&nbsp;</p>
<P class=MsoNormal ><input type=submit name=Login value="Login" style="padding:0 10pt 0 10pt"></P>
</form>
</body>
</html>
